{layout name="public/layout" /}
{include file='yxnavbar'}


{include file='../../plugin/shopplan/template/web/shopplan_common_nav'}
<style type="text/css">
.plan_wrap,.special{
	background: rgba(0,0,0,0.5);
	position: fixed;
	top: 0;
	bottom: 0;
	left: 0;
	right: 0;
}
.plan_wrap #form2,.special #form3{
	position: relative;
	position: absolute;
	top: 0;
	bottom: 0;
	left: 220px;
	right: 0;
}
.plan_wrap #form2>div,.special #form3>div{

	display: block;
	width: 80%;
	top: 10%;
	bottom: 10%;
	left: 0;
	right: 0;
	background: #ffffff;
	overflow: scroll;
	margin: auto;
	position: absolute;
}
.plan_wrap #form2 select,.special #form3 select{
	width: 100%;
}
.we7-form .control-label, form .control-label{
	padding-left: 15px
}
.panel.panel-default>div{
	min-width: 900px;
}
</style>
{if $system['is_shop_plan']==1}

<script type="text/javascript">
function changeStore(){
    var themeid = $('#themeid option:selected').val();
	var postUrl = "{php echo createWebUrlPlugin('shopplan.shops',array('themeid'=>$themeid));}";
        postUrl+='&themeid='+themeid;
    window.location.href = postUrl;
}
</script>
<form id="form1" class="form form-horizontal" action="{php echo createWebUrlPlugin('shopplan.shops',array('id'=>$id,'themeid'=>$themeid,'storeid'=>$storeid));}" enctype ="multipart/form-data" method="post"> 

<div class="main">
	<div class="tab-content" style="">
		<div class="tab-pane active" id="basic">
		
			<div class="panel panel-default" role="tabpanel" style="">
				<div class="panel-heading">店铺管理
                    {if count($themelist)>1 }
                    <div class="col-xs-12 col-sm-2 col-md-2" style="float: right;margin-top: -7px">
                        <select onchange="changeStore();" class="form-control" name="themeid" id="themeid">
                            {loop $themelist $row}
                            <option value="{$row['id']}" {if $themeid == $row['id'] }selected="selected"{/if}>{$row['id']} - {php echo mb_substr($row['title'],0,8,'utf-8');}</option>
                            {/loop}
                        </select>
                    </div>
                    {else}
                    <input type="hidden" name="themeid" value="{$themeid}" />
                    {/if}

				</div>
				<div class="panel-body" style="">
				
				
					<div class="form-group">
						<label class="col-sm-2 col-xs-12 control-label"></label>
						<div class="col-xs-12">
							<table class='table table-hover'>
								<thead>
								<tr>
									<th width="12%">分店名称</th>
									<th width="12%">分店地址</th>
									<th width="12%">分店电话</th>
									<th colspan="3">操作</th>
									
								</tr>
								</thead>
								<tbody id="plan_shop_config_items">
								<tr>
									<td><input name="infos[0][name]" type="text" class="form-control span3" value="{$shops['name']}"/></td>
									<td><input name="infos[0][address]" type="text" class="form-control span3" value="{$shops['address']}"/></td>
									<td><input name="infos[0][phone]" type="text" class="form-control span3" value="{$shops['phone']}"/></td>
									<td  {if empty($shops['name']) || empty($shops['address']) || empty($shops['phone'])} style="display:none;"{/if} >
										<!-- <a href="{php echo createWebUrlPlugin('shopplan.setplan',array('themeid'=>$themeid,'shopid'=>'0'));}" style="background-color: #5cb85c; padding: 10px 10px 11px 10px;border-radius:5px;color: white;">设置时间规划</a> -->
										<a href="javascript:isShowPlan({$shops['shopid']})" style="background-color: #5cb85c; padding: 10px 10px 11px 10px;border-radius:5px;color: white;">门店规划</a>
										<a href="javascript:isShowSpecial({$shops['shopid']})" style="background-color: #5cb85c; padding: 10px 10px 11px 10px;border-radius:5px;color: white;">特殊日期</a>
									</td>

									<td>总店<input type="hidden" name="infos[0][like]" value="1"></td>
									<td><a  href='javascript:;' onclick="shopchange({$themeid},{$shops['shopid']})" style="background-color: green; padding: 3px 10px;border-radius:5px; text-align:center;color: white;">保存</a></td>
									<!-- <td><input  name="infos[0][shopid]" type="hidden" value="{$shops['shopid']}" /><a  href='javascript:;' style="display:none">删除</a></td> -->
								</tr>
								{loop $shopconfig $key $shop_item}
								{php $key2=$key+1;}
								<tr >
									<td><input name="infos[{$key2}][name]" type="text" class="form-control span3" value="{$shop_item['name']}"/></td>
									<td><input name="infos[{$key2}][address]" type="text" class="form-control span3" value="{$shop_item['address']}"/></td>
									<td><input name="infos[{$key2}][phone]" type="text" class="form-control span3" value="{$shop_item['phone']}"/></td>
								
									<td >{if $shop_item['like']==0}
									<!-- <a href="{php echo createWebUrlPlugin('shopplan.setplan',array('themeid'=>$themeid,'shopid'=>$shop_item['shopid']));}" style="background-color: #5cb85c; padding: 10px 10px 11px 10px;border-radius:5px; text-align:center;color: white;" >门店规划</a> -->
									<a href="javascript:isShowPlan({$shop_item['shopid']})" style="background-color: #5cb85c; padding: 10px 10px 11px 10px;border-radius:5px; text-align:center;color: white;">门店规划</a>
										<a href="javascript:isShowSpecial({$shop_item['shopid']})" style="background-color: #5cb85c; padding: 10px 10px 11px 10px;border-radius:5px;color: white;">特殊日期</a>{/if}
									</td>
								
									<td>与总店相同：<input type="radio" name="infos[{$key2}][like]" value="1" {if $shop_item['like']==1}checked="checked"{/if}>是&nbsp;<input type="radio" name="infos[{$key2}][like]" value="0" {if $shop_item['like']==0}checked="checked"{/if}>否</td>
									<td><input  name="infos[{$key2}][shopid]" type="hidden" value="{$shop_item['shopid']}" id="getshopid"/><a href='javascript:;' style="background-color: green; padding: 3px 10px;border-radius:5px; text-align:center;color: white;" onclick="shopchange({$themeid},{$shop_item['shopid']})">保存</a><a href='javascript:;' style="background-color: #ca4242; padding: 3px 10px;border-radius:5px; text-align:center;color: white;margin-left: 10px" onclick='removeShopConfigItem(this,{$shop_item['shopid']},{$themeid})'>删除</a></td>
									
								</tr>
								{/loop}
								</tbody>
							</table>							
							
							
							
						</div>

	<span class="help-block">
		<a href="javascript:;" onclick="addShopConfig();" style="background-color:#428bca;padding: 11px 20px; color: white;border-radius:5px;margin-left: 23px"><i class="glyphicon glyphicon-plus" title="addrules"></i>添加分店</a>
	</span>
					</div>

			
					
					
</div></div></div></div></div>


<!-- <input name="submit" type="submit" value="提交设置" class="btn btn-primary col-lg-2"> -->
</form>	

<div class="plan_wrap" style="display: none">

</div>
<div class="special" style="display: none">

</div>
{/if} 
</div>
<div id="hide_html" style="display: none">
<div class="form-group">
            	<div class="col-sm-12 col-xs-12">
                   <label class="col-sm-3" id="yx_s_work00s">{php echo view_form_field_date('plan_s_work01_time[workdays][]',$tomorrow);}</label>
	                <div class="col-sm-3 col-xs-12" id="yx_add_s_work00s">
							<select name="plan_s_work01_time[tplid][]">
								<option disabled selected>选择时间规划</option>
								<option value="holiday">休息日</option>
								{loop $tplitem $k $tplitems}
									<option value="{$tplitems['id']}">{$tplitems['tplname']}</option>
								{/loop}
							</select>	
							
	                </div>	
	                
					<label class="col-sm-3 col-xs-12 control-label">
<!-- 						<a style="background-color: green; padding: 3px 10px;border-radius:5px; text-align:center;color: white;" href="" onclick="if(!confirm('删除后将不可恢复，确定删除吗?')) return false;">保存</a> -->
						<a style="background-color: #ca4242; padding: 3px 10px;border-radius:5px; text-align:center;color: white;" onclick="delSpecial2(this)">删除</a>
					</label>	
               </div>		
            </div>
</div>
<script>
	//门店规划
	function isShowPlan(flag){
		if(flag){
			$(".plan_wrap").empty();
			var shopid=flag;
		    var postUrl = "{php echo createWebUrlPlugin('shopplan.setplan',array('themeid'=>$themeid));}";
		        postUrl+="&shopid="+shopid;

	        $.ajax({
	            url:postUrl,
	            dataType:'json',
	            method:'post',
	            success:function (e) {
	                var status = e.error;
		            var ruleBtn = e.data.data;
	                if (status!=0){
	                    layer.msg(e.msg)
	                }else{
	                    // layer.msg(e.msg);
						$(".plan_wrap").append(ruleBtn);
						$(".plan_wrap").show();
	                }
	            }
	        });
		}else{
			$(".plan_wrap").empty();
			$(".plan_wrap").hide();
		}
	}
	//提交门店规划
	function sendForm(id,themeid,shopid){
    	var postUrl = "{php echo createWebUrlPlugin('shopplan.plansave',array('themeid'=>$themeid));}";
    	var formData = $("#form2").serialize();
    		postUrl+='&id='+id;
    		postUrl+='&shopid='+shopid;
// console.log(formData);
// return;

	    $.ajax({
	      type:'POST',
	      dataType:'json',
	      url:postUrl,
	      data:formData,
	      success:function (res){
	        isShowPlan(shopid);
	      }
	    });

	}
	//提交特殊日期
	function sendSpecial(id,themeid,shopid){
    	var postUrl = "{php echo createWebUrlPlugin('shopplan.specialsave',array('themeid'=>$themeid));}";
    	var formData = $("#form3").serialize();
    		postUrl+='&id='+id;
    		postUrl+='&shopid='+shopid;
    		// console.log(formData);
    		// return;

	    $.ajax({
	      type:'POST',
	      dataType:'json',
	      url:postUrl,
	      data:formData,
	      success:function (res){
	        isShowSpecial(shopid);
	      }
	    });
	}
	//特殊日期管理
	function isShowSpecial(flag){
		if(flag){
			$(".special").empty();
			var shopid=flag;
		    var postUrl = "{php echo createWebUrlPlugin('shopplan.showspecial',array('themeid'=>$themeid));}";
		        postUrl+="&shopid="+shopid;

	        $.ajax({
	            url:postUrl,
	            dataType:'json',
	            method:'post',
	            success:function (e) {
	                var status = e.error;
		            var ruleBtn = e.data.data;
	                if (status!=0){
	                    layer.msg(e.msg)
	                }else{
	                    // layer.msg(e.msg);
						$(".special").append(ruleBtn);
						$(".special").show();
	                }
	            }
	        });
		}else{
			$(".special").empty();
			$(".special").hide();
		}
	}
	//删除特殊日期
	function delSpecial(themeid,shopid,special_rules_id){
		if(!confirm('删除后将不可恢复，确定删除吗?')) return false;
// array('themeid'=>$themeid,'special_rules_id'=>$k1,'shopid'=>$shopid)

	    var postUrl = "{php echo createWebUrlPlugin('shopplan.delspecials',array('themeid'=>$themeid));}";
	        postUrl+="&shopid="+shopid;
	        postUrl+="&special_rules_id="+special_rules_id;

        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            success:function (e) {
                var status = e.error;
                if (status!=0){
                    layer.msg(e.msg)
                }else{
                    // layer.msg(e.msg);
					isShowSpecial(shopid)
                }
            }
        });
	}
	//删除还没有入库的特殊日期
    function delSpecial2(obj) {
		$(obj).parent().parent().parent().remove();
    }
	function addSpecial(){
		// var k =$(".special").find('select').size();
		// k=k+1;
		// console.log(k);

		$(".special .panel-body").append($("#hide_html").html());
	}
	//增加店铺
    function addShopConfig() {
		var k =$("#plan_shop_config_items").find('tr').size();
		var k =k+{$addsum};
        var html = "<tr>";
        html += '<td><input name="infos[' +k+'][name]" type="text" class="form-control span3" value="" /></td>';
        html += '<td><input name="infos[' +k+'][address]" type="text" class="form-control span3" value="" /></td>';
        html += '<td><input name="infos[' +k+'][phone]" type="text" class="form-control span3" value="" /></td>';
		html += '<td style="display:none;"><a href="{php echo createWebUrlPlugin('shopplan.setplan',array('themeid'=>$themeid,'shopid'=>'k'));}">门店规划</a></td>';
		html += '<td>与总店相同：<input type="radio" name="infos['+k+'][like]" value="1" >是&nbsp;<input type="radio" name="infos['+k+'][like]" value="0" checked="checked">否</td>';
		html += '<td> <input  name="infos['+k+'][shopid]" type="hidden" value="'+k+'"  id="getshopid"/>&nbsp;<a href="javascript:;" style="background-color: green; padding: 3px 10px;border-radius:5px; text-align:center;color: white;" onclick="shopchange2({$themeid},'+k+')">保存</a><a style="background-color: #ca4242; padding: 3px 10px;border-radius:5px; text-align:center;color: white;" href="javascript:;" onclick="removeShopConfigItem2(this)" >删除</a></td>';
        html += "</tr>";
        $("#plan_shop_config_items").append(html);
    }
	//删除库中的店铺数据
    function removeShopConfigItem(obj,shopid,storeid) {
		if(!confirm('删除后将不可恢复，确定删除吗?')) return false;
	
	    var postUrl = "{php echo createWebUrlPlugin('shopplan.delconfig',array('id'=>$id,'themeid'=>$themeid,'storeid'=>$storeid));}";
			postUrl +="&shopid="+shopid;
        var success_redirect = "{php echo createWebUrlPlugin('shopplan.shops',array('id'=>$id,'themeid'=>$themeid,'storeid'=>$storeid));}";
        var formData = $("#form1").serialize();

        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:formData,
            success:function (e) {
                var status = e.status;
                var result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
					$(obj).parent().parent().remove();
                    window.location.href = success_redirect;
                }
            }
        })
    }
	//删除还没有入库的店铺数据
    function removeShopConfigItem2(obj) {
		$(obj).parent().parent().remove();
    }
	//保存门店信息
    function shopchange(themeid,shopid) {
    	var k = shopid-1;
		var shopname = $("input[name='infos["+k+"][name]'] ").val();
		var shopaddress = $("input[name='infos["+k+"][address]'] ").val();
		var shopphone = $("input[name='infos["+k+"][phone]'] ").val();
		var shoplike = $("input[name='infos["+k+"][like]']:checked").val();
		if (!shopname || shopname.length<1){
			layer.msg('分店名称不能为空');
			return;
		}
		if (!shopaddress || shopaddress.length<1){
			layer.msg('分店地址不能为空');
			return;
		}
		console.log(shopphone.length)
		var reg_mobile = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
		var reg_tel = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
		if (!shopphone){
			layer.msg('分店电话不能为空');
			return;
		}else{
			/*if (shopphone.length!=12 && shopphone.length!=11){
				layer.msg('分店电话格式不正确');
				return;
			}*/
			// if (shopphone.length==8){
			// 	layer.msg('请添加区号');
			// 	return;
			// }
			//
			// if ( !reg_mobile.test(shopphone) && !reg_tel.test(shopphone)){
			// 	layer.msg('请输入正确的手机号或座机号');
			// 	return;
			// }
		}
	    var postUrl = "{php echo createWebUrlPlugin('shopplan.editshop',array('themeid'=>$themeid));}";
        var success_redirect = "{php echo createWebUrlPlugin('shopplan.shops',array('themeid'=>$themeid));}";
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
			data:{shopid:shopid,name:shopname,address:shopaddress,phone:shopphone,like:shoplike},
            success:function (e) {
                var status = e.status;
                var result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
                    window.location.href = success_redirect;
                }
            }
        })
    }	
	//保存门店信息
    function shopchange2(themeid,k) {
    	var shopid = k+1;
		var shopname = $(" input[ name='infos["+k+"][name]' ] ").val();
		var shopaddress = $(" input[ name='infos["+k+"][address]' ] ").val();
		var shopphone = $(" input[ name='infos["+k+"][phone]' ] ").val();
		var shoplike = $(" input[ name='infos["+k+"][like]']:checked ").val();

		if (!shopname || shopname.length<1){
			layer.msg('分店名称不能为空');
			return;
		}
		if (!shopaddress || shopaddress.length<1){
			layer.msg('分店地址不能为空');
			return;
		}

		var reg_mobile = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
		var reg_tel = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
		if (!shopphone){
			layer.msg('分店电话不能为空');
			return;
		}else{
			/*if (shopphone.length!=8 && shopphone.length!=11){
				layer.msg('分店电话格式不正确');
				return;
			}
			if (shopphone.length==8){
				layer.msg('请添加区号');
				return;
			}*/
			if ( !reg_mobile.test(shopphone) && !reg_tel.test(shopphone)){
				layer.msg('请输入正确的分店手机号或座机号');
				return;
			}
		}

	    var postUrl = "{php echo createWebUrlPlugin('shopplan.editshop',array('themeid'=>$themeid));}";
        var success_redirect = "{php echo createWebUrlPlugin('shopplan.shops',array('themeid'=>$themeid));}";

        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
			data:{shopid:shopid,name:shopname,address:shopaddress,phone:shopphone,like:shoplike},
            success:function (e) {
                var status = e.status;
                var result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
                    window.location.href = success_redirect;
                }
            }
        })
    }	
</script>		
